from PIL import Image
import numpy as np

def compress_bmp_to_16x16(image_path):
    # 打开BMP图片
    img = Image.open(image_path)
    
    # 将图像压缩到16x16分辨率，使用 LANCZOS 代替 ANTIALIAS
    img_resized = img.resize((16, 16), Image.LANCZOS)
    
    # 转换为RGB888格式（确保图像是RGB模式）
    img_rgb = img_resized.convert('RGB')
    
    # 创建一个16x16的3D数组来保存RGB888颜色数据
    display_data = np.zeros((16, 16, 3), dtype=np.uint8)
    
    # 将图像的像素值存储到数组中
    for row in range(16):
        for col in range(16):
            r, g, b = img_rgb.getpixel((col, row))
            display_data[row][col][0] = r  # 红色通道
            display_data[row][col][1] = g  # 绿色通道
            display_data[row][col][2] = b  # 蓝色通道
    
    return display_data

def print_as_c_array(data):
    # 打印C语言的数组
    print("uint8_t display_data[16][16][3] = {")
    for row in range(16):
        print("    {", end="")
        for col in range(16):
            r, g, b = data[row][col]
            if col < 15:
                print(f"{{{r}, {g}, {b}}}, ", end="")
            else:
                print(f"{{{r}, {g}, {b}}}", end="")
        if row < 15:
            print("},")
        else:
            print("}")
    print("};")

# 调用函数并生成C数组
image_path = 'images.png'  # 替换为你的BMP文件路径
data = compress_bmp_to_16x16(image_path)
print_as_c_array(data)
